/**
 * 
 */
package com.objectdraw.server.commands;

import com.objectdraw.server.CommunicationServer;
import com.objectdraw.server.ObjectDrawServer;
import com.objectdraw.server.data.RequestData;
import com.objectdraw.server.data.ResponseData;

/**
 * This class processes color change requests from the client and ensures the
 * object draw server is updated according to what the active drawer has
 * requested
 * 
 * @author Harrison
 */
public class ColorChangeCommand extends Command {
	static final private String myCommand = "color";

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.objectdraw.server.commands.Command#processRequest(com.objectdraw.server.RequestData,
	 *      com.objectdraw.server.ResponseData)
	 */
	/**
	 * takes a message that has a color command and changes the server color setting to match
	 * the parameters of the message. also passes the color change message on to all non-controlling
	 * clients 
	 */
	@Override
	public void processRequest(RequestData request, ResponseData response) {
		if (myCommand.equalsIgnoreCase(request.getCommand())) {
			System.out.println(myCommand + " Command");

			ObjectDrawServer serv = ObjectDrawServer.getInstance();
			String params[] = request.getParams().split(" ");
			if (params.length > 1) {
				String uname = params[1];
				if (uname.equalsIgnoreCase(serv.getCurrentDrawer())) {
					serv.setCurrentColor(params[0]);
					serv.colorSelected();

					ResponseData res = new ResponseData();
					res.addResponse("color " + params[0]);
					CommunicationServer.broadcast(res, ObjectDrawServer
							.getInstance().getViewingClientSockets());
				}
			}
		}

		// Pass the request on to the next person in line
		if (this.successor != null) {
			this.successor.processRequest(request, response);
		}
	}

}
